package cn.lxg.service.impl;

import cn.lxg.dao.INovelChapterDao;
import cn.lxg.pojo.TNovelChapter;
import cn.lxg.service.INovelChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class NovelChapterServiceImpl implements INovelChapterService {

    @Autowired
    private final INovelChapterDao novelChapterDao = null;

    @Override
    @Transactional
    public int countDataLine(TNovelChapter novelChapter) {
        return this.novelChapterDao.countDataLine(novelChapter);
    }

    @Override
    @Transactional
    public List<TNovelChapter> getList(Integer limit, Integer page, TNovelChapter novelChapter) {
        /* 计算页面 */
        Integer limitStart = null;
        Integer limitEnd = null;
        if (limit != null && page != null) {
            limitStart = (page - 1) * limit;
            limitEnd = limit;
        }
        return this.novelChapterDao.select(limitStart, limitEnd, novelChapter);
    }

    @Override
    @Transactional
    public TNovelChapter get(Integer id) {
        return this.novelChapterDao.findById(id);
    }

    @Override
    @Transactional
    public String add(TNovelChapter novelChapter) {
        // 数据校验
        TNovelChapter affirmNovelChapter = new TNovelChapter();
        affirmNovelChapter.setNovelId(novelChapter.getNovelId());
        affirmNovelChapter.setIndex(novelChapter.getIndex());
        if (this.novelChapterDao.countDataLine(affirmNovelChapter) != 0)
            return "添加章节失败，已经存在同章节了";

        novelChapter.setUpdateTime(new Date());
        novelChapter.setCreateTime(new Date());
        if (this.novelChapterDao.insert(novelChapter))
            return "添加章节成功";
        else
            return "添加章节失败";
    }

    @Override
    @Transactional
    public String delete(Integer id) {
        if (this.novelChapterDao.deleteById(id))
            return "删除章节成功";
        else
            return "删除章节失败";
    }

    @Override
    @Transactional
    public String edit(TNovelChapter novelChapter) {
        // 查旧文章
        TNovelChapter oldNovelChapter = this.novelChapterDao.findById(novelChapter.getId());

        // 数据校验
        if (oldNovelChapter.getIndex() != novelChapter.getIndex()) {
            TNovelChapter affirmNovelChapter = new TNovelChapter();
            affirmNovelChapter.setNovelId(novelChapter.getNovelId());
            affirmNovelChapter.setIndex(novelChapter.getIndex());
            if (this.novelChapterDao.countDataLine(affirmNovelChapter) != 0)
                return "编辑章节失败，已经存在同序号的章节了";
        }

        // 更新属性
        oldNovelChapter.setIndex(novelChapter.getIndex());
        oldNovelChapter.setName(novelChapter.getName().trim());
        oldNovelChapter.setContent(novelChapter.getContent());
        oldNovelChapter.setUpdateTime(new Date());

        // 保存数据
        if (this.novelChapterDao.updateById(oldNovelChapter))
            return "编辑章节成功";
        else
            return "编辑章节失败";
    }
}
